package com.ebelter.seneo.ui.fragments.bonddevices;

import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.ebelter.btcomlib.models.bluetooth.base.BlueScan;
import com.ebelter.btcomlib.models.bluetooth.interfaces.IScanCallback;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.ui.activitys.SeneoBondDeviceActivity;
import com.ebelter.seneo.ui.fragments.BaseFragment;
import java.util.ArrayList;
import java.util.List;

import static com.ebelter.seneo.ui.activitys.SeneoBondDeviceActivity.F_SCAN_FAILURE;


public class BondScanDevicesFragment extends BaseFragment {
    public static final String TAG = "BondScanDevicesFragment";

    ListView search_pair_lv;
    BondAdapter mBondAdapter;
    BlueScan mBlueScan;

    List<BluetoothDevice> blueList;
    @Override
    protected int setLayoutResouceId() {
        return R.layout.bondscan_fly;
    }


    @Override
    protected void initView() {
        search_pair_lv=findViewById(R.id.search_pair_lv);
        search_pair_lv.setAdapter(mBondAdapter);

        search_pair_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mBondAdapter.checkAddress = blueList.get(position).getAddress();
                mBondAdapter.checkName = blueList.get(position).getName();
                mBondAdapter.notifyDataSetChanged();
                if ((getActivity()) != null) {
                    ((SeneoBondDeviceActivity)getActivity()).setNextBt(true);
                }
            }
        });
    }

    @Override
    protected void initData(Bundle arguments) {

    }

    public void initDataFile(Object o){
        mBlueScan = new BlueScan(o);
        mBlueScan.setScanTime(24000);
        mBlueScan.setScanCallback(mIScanCallback);
        blueList = new ArrayList<>();
        mBondAdapter = new BondAdapter();
    }


    private final IScanCallback mIScanCallback=new IScanCallback() {
        @Override
        public void beforeScan() {

            LogUtils.i(TAG,"--------开始扫描");

        }

        @Override
        public void scanOneDevice(BluetoothDevice device, int rssi, byte[] scanRecord) {
            if (device==null|| TextUtils.isEmpty(device.getName())||TextUtils.isEmpty(device.getAddress())){
                return;
            }

            LogUtils.i(TAG,"--------扫描到--Name="+device.getName()+"--Address="+device.getAddress());

            if (TextUtils.equals(device.getName(),"Body Fat-B2")||TextUtils.equals(device.getName(),"Body Fat-B1")){



            String address = device.getAddress();
            boolean isC = false;
            if (blueList!=null){
                for (int i = 0;i<blueList.size();i++){
                    if(TextUtils.equals(address,blueList.get(i).getAddress())){
                        isC=true;
                        break;
                    }
                }
                if (!isC){
                    if (mBondAdapter!=null){
                        blueList.add(device);
                        mBondAdapter.notifyDataSetChanged();
                    }

                }
            }


            }

        }

        @Override
        public void overScan() {
            LogUtils.i(TAG,"--------扫描结束");
            //扫描结束
            if (mBondAdapter!=null){
                if(mBondAdapter.getCount()==0){
                    if ((getActivity()) != null) {
                        ((SeneoBondDeviceActivity)getActivity()).displayFragment(F_SCAN_FAILURE);
                    }
                }else {
                    mBlueScan.startScanBluetoothDevices();
                }
            }


        }
    };


    @Override
    public void onDestroy() {
        super.onDestroy();
        blueList.clear();

        mBondAdapter=null;
        mBlueScan.stopScanBluetoothDevices();
        mBlueScan=null;
    }

    //开始扫描
    public void startScan() {
        if (blueList!=null)blueList.clear();
        if (mBondAdapter!=null) {
            mBondAdapter.notifyDataSetChanged();
        }
        if (mBlueScan!=null){mBlueScan.startScanBluetoothDevices();}
    }


    public class BondAdapter extends BaseAdapter{


        public String checkAddress;
        public String checkName;
        @Override
        public int getCount() {
            return blueList==null?0:blueList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Vh vh;
            if (convertView==null){
                vh=new Vh();
                convertView=View.inflate(getContext(),R.layout.item_searchblue_ly,null);
                vh.name=convertView.findViewById(R.id.item_searchblue_title);
                vh.address=convertView.findViewById(R.id.item_searchblue_address);
                vh.iv=convertView.findViewById(R.id.item_searchblue_iv);
                convertView.setTag(vh);

            }else {
                vh= (Vh) convertView.getTag();
            }


           // vh.name.setText(blueList.get(position).getName());
            vh.name.setText("Seneo Scale");
            vh.address.setText("<"+blueList.get(position).getAddress()+">");

            if (TextUtils.isEmpty(checkAddress)){
                ViewUtils.hideView(vh.iv);
            }else {
                if(TextUtils.equals(blueList.get(position).getAddress(),checkAddress)){
                    ViewUtils.displayView(vh.iv);
                }else {
                    ViewUtils.hideView(vh.iv);
                }

            }
            return convertView;
        }


        class Vh{
            TextView name;
            TextView address;
            ImageView iv;
        }
    }


    public String[] getCheckBluetooth(){
        if (mBondAdapter!=null){
            String add=mBondAdapter.checkAddress;
            String name=mBondAdapter.checkName;
            if (!TextUtils.isEmpty(add)&&!TextUtils.isEmpty(name)){
                String[] r=new String[2];
                r[0]=add;
                r[1]=name;
                return r;
            }
        }
        return null;
    }


    public BondAdapter getmBondAdapter() {
        return mBondAdapter;
    }
}
